package com.fht.spring_boot_study.model.common.controller;

import com.fht.spring_boot_study.model.common.entity.ImageType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class DictionaryController {
    private static Map<String,Class> enumMap=new HashMap(){{
        put("imageType",ImageType.class);
    }};

    @GetMapping("/dictionary/{name}")
    public List<Object> getEnum(@PathVariable String name){

            try {
                if (enumMap.containsKey(name)) {
                    Class clazz = enumMap.get(name);
                  Method method= clazz.getDeclaredMethod("values");
                 Object[] objects=(Object[]) method.invoke(null);
                 return Arrays.asList(objects);
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }


        return null;
    }
}
